* THIS DO-FILE GENERATES VARIABLES FOR PARENT IDS AND PARENTAL WEALTH (ASSETS) BASED ON INC AND BEF REGISTRIES

global raw "Path to rawdata"

use person-panel_v7, clear
keep pnr birthyear MOR_ID FAR_ID
ren birthyear focal_birthyear
gsort pnr -MOR_ID -FAR_ID
ren MOR_ID o_MOR_ID
ren FAR_ID o_FAR_ID
duplicates drop pnr, force
sort pnr
save tempparents, replace

set more off
forvalues i = 1986/2015 {
	di `i'
	use tempparents, clear
	gen year=`i'
	merge 1:1 pnr using $raw/bef`i', nogen norep keep(mat mas) keepus(MOR_ID FAR_ID)
	if year>1986 {
		qui append using temp.dta, force
		}
	qui save temp.dta, replace
	}
sort pnr year
replace MOR_ID=MOR_ID[_n-1] if pnr==pnr[_n-1] & MOR_ID=="" & MOR_ID[_n-1]!=""
replace FAR_ID=FAR_ID[_n-1] if pnr==pnr[_n-1] & FAR_ID=="" & FAR_ID[_n-1]!=""
replace MOR_ID=MOR_ID[_n+1] if pnr==pnr[_n+1] & MOR_ID=="" & MOR_ID[_n+1]!=""
replace FAR_ID=FAR_ID[_n+1] if pnr==pnr[_n+1] & FAR_ID=="" & FAR_ID[_n+1]!=""
gsort pnr -year
replace MOR_ID=MOR_ID[_n-1] if pnr==pnr[_n-1] & MOR_ID=="" & MOR_ID[_n-1]!=""
replace FAR_ID=FAR_ID[_n-1] if pnr==pnr[_n-1] & FAR_ID=="" & FAR_ID[_n-1]!=""
replace MOR_ID=MOR_ID[_n+1] if pnr==pnr[_n+1] & MOR_ID=="" & MOR_ID[_n+1]!=""
replace FAR_ID=FAR_ID[_n+1] if pnr==pnr[_n+1] & FAR_ID=="" & FAR_ID[_n+1]!=""
sort pnr year
duplicates drop pnr, force
drop year o_*

save parents_id, replace
erase temp.dta
erase tempparents.dta

**** MOMS
use parents_id, clear
drop FAR_ID pnr
ren MOR_ID pnr
drop if pnr==""
duplicates drop pnr, force
save momstemp, replace
set more off
forvalues i = 1980/2015 {
	di `i'
	use momstemp, clear
	gen year=`i'
	if year>=1986 {
		merge 1:1 pnr using $raw/bef`i', nogen norep keep(mat mas) keepus(FOED_AAR)
	}
	merge 1:1 pnr using $raw/ind`i', nogen norep keep(mat mas)
	if year>1980 {
		qui append using temp.dta, force
		}
	qui save temp.dta, replace
}
erase temp.dta
erase momstemp.dta
sort pnr year
gen assets=qaktivf
replace assets=QAKTIVF_NY05 if assets==. | (assets<QAKTIVF_NY05 & QAKTIVF_NY05!=. & assets!=.)
replace assets=0 if assets<0
compress
save momsraw, replace

**** DADS
use parents_id, clear
drop MOR_ID pnr
ren FAR_ID pnr
drop if pnr==""
duplicates drop pnr, force
save dadstemp, replace
set more off
forvalues i = 1980/2015 {
	di `i'
	use dadstemp, clear
	gen year=`i'
	if year>=1986 {
		merge 1:1 pnr using $raw/bef`i', nogen norep keep(mat mas) keepus(FOED_AAR)
	}
	merge 1:1 pnr using $raw/ind`i', nogen norep keep(mat mas)
	if year>1980 {
		qui append using temp.dta, force
		}
	qui save temp.dta, replace
}
erase temp.dta
erase dadstemp.dta
sort pnr year
sort pnr year
gen assets=qaktivf
replace assets=QAKTIVF_NY05 if assets==. | (assets<QAKTIVF_NY05 & QAKTIVF_NY05!=. & assets!=.)
replace assets=0 if assets<0
compress
save dadsraw, replace

****** RANK
use momsraw, clear
gen mom_age=year-FOED_AAR
bys year: egen n=count(assets)
bys year: egen i=rank(assets)
gen mom_pc_assets=(i-1)/(n-1)*100
drop i n
ren assets mom_assets
ren pnr MOR_ID
keep MOR_ID year mom_*
save momsincome, replace

use dadsraw, replace
gen dad_age=year-FOED_AAR
bys year: egen n=count(assets)
bys year: egen i=rank(assets)
gen dad_pc_assets=(i-1)/(n-1)*100
drop i n
ren assets dad_assets
ren pnr FAR_ID
keep FAR_ID year dad_*
save dadsincome, replace

**** BOTH
use dadsincome, clear
keep year
duplicates drop year, force
save tempyear, replace

use parents_id, clear
cross using tempyear
joinby FAR_ID year using dadsincome, unm(m)
drop _merge
joinby MOR_ID year using momsincome, unm(m)
drop _merge
gen assets=dad_assets+mom_assets
bys year: egen n=count(assets)
bys year: egen i=rank(assets)
gen par_pc_assets=(i-1)/(n-1)*100
drop i n
ren assets par_assets
gen focal_age=year-focal_birthyear
collapse (median) par_*, by(pnr)
* PARENTAL ASSETS IS THE MEDIAN (OVER YEARS) OF THE ANNUAL PERCENTILE RANK OF PARENTS IN THE DISTRIBUTION OF PARENTAL ASSETS.
sort pnr
save parents_v6, replace

erase tempyear.dta
erase momsincome.dta
erase dadsincome.dta





